# 負責顯示客戶端瀏覽功能的控制器.
class ShowController < ApplicationController
  
  # 除了加入最愛電報/清空最愛電報/從最愛電報中移除電報/顯示最愛電報 外,
  # 其它方法都要先紀錄目前的 URL .
  before_filter :record_uri, :except => [:add_to_favorite, 
    :clear_favorite, 
    :remove_from_favorite]
  
  # 所有方法都要找到最愛電報清單 .
  before_filter :find_favorite
  
  
  # 將電報加入最愛電報清單
  def add_to_favorite
    begin
      computer = Computer.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      logger.error("使用者嘗試存取錯誤的電報 #{params[:id]}")
#      flash[:notice] = "查無此電報"
      redirect_to_error_page
    else
      @favorite.add_computer(computer)
      
      uri = session[:original_uri]
      session[:original_uri] = nil;
      redirect_to uri
    end
  end
  
  # 清空 Session 中的最愛清單.
  def clear_favorite
    @favorite.clear_all
#    session[:favorite] = nil # 清除 Session 中的最愛電報.
    
    uri = session[:original_uri]
    session[:original_uri] = nil;
    redirect_to uri
  end
  
  # 將電報從最愛電報清單中移除.
  def remove_from_favorite
    computer = Computer.find(params[:id])
    @favorite.remove_computer(computer)
    
    uri = session[:original_uri]
    session[:original_uri] = nil;
    redirect_to uri
  end
  
  # 顯示最愛電報中的所有電報.
  def show_favorite
    @computers = @favorite.computers
  end

  # 導向歡迎畫面.
  def index
#    redirect_to "http://tangblack.googlepages.com/%E9%9B%BB%E5%A0%B1%E5%AE%8C%E5%85%A8%E6%89%8B%E5%86%8A"
  end
  
  # 顯示總價未滿10000元的所有組裝報價.
  def lower_than_low
    @computers = 
      Computer.find_computers_less_than(
        @@low_pc_price, 
        params[:page], 
        @@per_page,
        @@recent,
        @@sort_by_updated_at_desc)
  end
  
  # 顯示總價介於10000元到20000元之間的所有組裝報價.
  def between_low_and_medium
    @computers = 
      Computer.find_computers_for_price(
        @@low_pc_price, 
        @@medium_pc_price, 
        params[:page], 
        @@per_page,
        @@sort_by_updated_at_desc)
  end
  
  # 顯示總價介於20000元到30000元之間的所有組裝報價.
  def between_medium_and_advanced
    @computers = 
      Computer.find_computers_for_price(
        @@medium_pc_price, 
        @@advanced_pc_price, 
        params[:page], 
        @@per_page,
        @@sort_by_updated_at_desc)
  end
  
  # 顯示總價超過30000元所有組裝報價.
  def higher_than_advanced
    @computers = 
      Computer.find_computers_more_than(
        @@advanced_pc_price, 
        params[:page], 
        @@per_page,
        @@sort_by_updated_at_desc)
  end
  
#  # 顯示所有組裝報價(由新到舊).
#  def index_sort_by_updated_at_desc
#    @computers = 
#      Computer.find_all_computers(
#        params[:page], 
#        @@per_page, 
#        @@recent,
#        @@sort_by_updated_at_desc)
#    
#    respond_to do |format|
#      format.html # index_sort_by_updated_at_desc.html.erb
#      format.xml  { render :xml => @computers }
#    end
#  end
#  
#  # 顯示所有組裝報價(由舊到新).
#  def index_sort_by_updated_at
#    @computers = 
#      Computer.find_all_computers(
#        params[:page], 
#        @@per_page, 
#        @@recent,
#        @@sort_by_updated_at)
#    
#    respond_to do |format|
#      format.html # index_sort_by_updated_at_desc.html.erb
#      format.xml  { render :xml => @computers }
#    end
#  end
#  
#  # 顯示所有組裝報價(由貴到便宜).
#  def index_sort_by_total_price
#    @computers = 
#      Computer.find_all_computers(
#        params[:page], 
#        @@per_page, 
#        @@recent,
#        @@sort_by_total_price)
#    
#    respond_to do |format|
#      format.html # index_sort_by_updated_at_desc.html.erb
#      format.xml  { render :xml => @computers }
#    end
#  end
#  
#  # 顯示所有組裝報價(由便宜到貴).
#  def index_sort_by_total_price_desc
#    @computers = 
#      Computer.find_all_computers(
#        params[:page], 
#        @@per_page, 
#        @@recent,
#        @@sort_by_total_price_desc)
#    
#    respond_to do |format|
#      format.html # index_sort_by_updated_at_desc.html.erb
#      format.xml  { render :xml => @computers }
#    end
#  end
  
  private
  
  @@per_page = 10 # 一頁呈現的資料筆數.
  @@low_pc_price = 10000
  @@medium_pc_price = 20000
  @@advanced_pc_price = 30000
  @@recent = 3.months.ago
  @@sort_by_total_price = 'total_price'
  @@sort_by_total_price_desc = 'total_price DESC'
  @@sort_by_updated_at = 'updated_at'
  @@sort_by_updated_at_desc = 'updated_at DESC'
  
end
